package com.newviews.tcmip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.StringUtils;
import com.newviews.tcmip.pojo.po.EtcmContackUsPo;
import com.newviews.tcmip.pojo.po.EtcmUiElPo;
import com.newviews.tcmip.dao.EtcmUiElMapper;
import com.newviews.tcmip.pojo.vo.ui.EtcmUiElRespVO;
import com.newviews.tcmip.service.inf.EtcmUiElServiceInf;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * UI设计-UI显示的元素 服务实现类
 * </p>
 *
 * @author lxc
 * @since 2024-02-29
 */
@Service
public class EtcmUiElServiceImpl extends ServiceImpl<EtcmUiElMapper, EtcmUiElPo> implements EtcmUiElServiceInf {

    @Resource
    private EtcmUiElMapper etcmUiElMapper;
    @Resource
    private ConfigProperties configProperties;
    @Override
    public ResultModel<List<EtcmUiElRespVO>> getData(String uiCode,String langCode) {
        String uiTypeCode = null;
        if (uiCode.equals(CommonConsts.UI_TYPE_CODE_PX_INGR)){
            uiTypeCode = CommonConsts.UI_TYPE_CODE_INGR;
        }

        if (uiCode.equals(CommonConsts.UI_TYPE_CODE_HERB_INGR)){
            uiTypeCode = CommonConsts.UI_TYPE_CODE_INGR;
        }

//        if (uiCode.equals(CommonConsts.UI_TYPE_CODE_HERB_EX)){
//            uiTypeCode = CommonConsts.UI_TYPE_CODE_PX_EX;
//        }

        if (uiCode.equals(CommonConsts.PRESCRIPTION_COMPOSITION_HERB)){
            uiTypeCode = CommonConsts.UI_HERB;
        }

        if (uiCode.equals(CommonConsts.INGREDIENT_ADMET)){
            uiTypeCode = CommonConsts.UI_TYPE_CODE_INGR;
        }
        String[] langArr = langCode.split(CommonConsts.SPIT);
        List<EtcmUiElPo> list = this.list(new QueryWrapper<EtcmUiElPo>().eq(EtcmUiElPo.UI_TYPE_CODE, StringUtils.isNotBlank(uiTypeCode) ? uiTypeCode : uiCode).in(EtcmUiElPo.LANGUAGE_CODE, langArr).eq(EtcmUiElPo.STATUS, CommonConsts.COMMON_YES).orderByAsc(EtcmUiElPo.UI_EL_SORT));
        if (uiCode.equals(CommonConsts.UI_TYPE_CODE_PX_INGR)){
            list = list.stream().filter(s -> s.getUiElSort() < 3 || (s.getUiElSort() > 6  && s.getUiElSort() < 12)).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
        }
        if (uiCode.equals(CommonConsts.UI_TYPE_CODE_HERB_INGR)){
            list = list.stream().filter(s -> s.getUiElSort() < 3 || (s.getUiElSort() > 6  && s.getUiElSort() < 12)).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
        }
        if (uiCode.equals(CommonConsts.UI_TYPE_CODE_INGR)){
            list = list.stream().filter(s -> s.getUiElSort() < 7).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
        }

        if (uiCode.equals(CommonConsts.UI_HERB)){
            list = list.stream().filter(s -> s.getUiElSort() != 3).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
        }
        if (uiCode.equals(CommonConsts.INGREDIENT_ADMET)){
            list = list.stream().filter(s -> s.getUiElSort() > 11).sorted(Comparator.comparing(EtcmUiElPo::getUiElSort)).collect(Collectors.toList());
        }
        List<EtcmUiElRespVO> collect = list.stream().map(workOrderVo -> {
            EtcmUiElRespVO exportVo = new EtcmUiElRespVO();
            BeanUtils.copyProperties(workOrderVo, exportVo);
            return exportVo;
        }).collect(Collectors.toList());
        return new ResultModel<List<EtcmUiElRespVO>>().success(collect);
    }

    @Override
    public ResultModel<List<EtcmContackUsPo>> getContackUs(String langCode) {
        List<EtcmContackUsPo> list = etcmUiElMapper.getContackUs(langCode);
        for (EtcmContackUsPo po : list) {

            po.setAvatar(configProperties.getAvatarFilePath() + po.getAvatar());
        }
        return new ResultModel<List<EtcmContackUsPo>>().success(list);
    }
}
